﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectHandlers
{
    public class GetProjects
    {

        private readonly ProjectRepository _projectRepository;

        #region Constructor
        public GetProjects()
        {
            _projectRepository = new ProjectRepository();
        }

        public GetProjects(ProjectRepository projectRepository)
        {
            _projectRepository = projectRepository;
        }
        #endregion

        #region Methods
        public virtual IEnumerable<ProjectModel> Execute()
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                projectData = _projectRepository.GetProjects();
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                return projectModel;

            }
            else
            {
                return null;
            }
        }

        public virtual IEnumerable<ProjectModel> Execute(string userId)
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                //projectData = _projectRepository.GetProjects();
                projectData = _projectRepository.GetProjectsByUser(userId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                return projectModel;

            }
            else
            {
                return null;
            }
        }

        public virtual IEnumerable<ProjectModel> Execute(string userId, decimal selectedProjectId)
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                projectData = _projectRepository.GetProjectsByUser(userId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                var result = new List<ProjectModel>();

                // calculating selected project
                foreach (ProjectModel model in projectModel)
                {
                    if (model.ProjectId == selectedProjectId)
                    {
                        model.Selected = "selected";
                    }
                    result.Add(model);
                }
                return result;
            }
            else
            {
                return null;
            }
        }

        public virtual List<ProjectModel> ExecuteToList(string userId)
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                projectData = _projectRepository.GetProjectsByUser(userId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                return projectModel.ToList();

            }
            else
            {
                return new List<ProjectModel>();
            }
        }

        public virtual List<ProjectModel> ExecuteToList()
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                projectData = _projectRepository.GetProjects();
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                return projectModel.ToList();

            }
            else
            {
                return new List<ProjectModel>();
            }
        }

        public virtual List<ProjectModel> ExecuteToList(string userId, decimal selectedProjectId)
        {
            IEnumerable<PROJECT> projectData = null;

            // try to get from db
            try
            {
                projectData = _projectRepository.GetProjectsByUser(userId);
            }
            catch (Exception ex)
            {
                LogService.Service.Debug(ex);
            }

            // try to parse to list
            if (projectData != null && projectData.Count() > 0)
            {
                var projectModel = from prj in projectData
                                   select new ProjectModel(prj);
                var result = new List<ProjectModel>();

                // calculating selected project
                foreach (ProjectModel model in projectModel)
                {
                    if (model.ProjectId == selectedProjectId)
                    {
                        model.Selected = "selected";
                    }
                    result.Add(model);
                }
                return result;
            }
            else
            {
                return new List<ProjectModel>();
            }
        }

        #endregion

    }
}
